package io.gitee.zhangbinhub.acp.cloud

import io.gitee.zhangbinhub.acp.boot.tools.ServerTools
import io.gitee.zhangbinhub.acp.cloud.aspect.RestControllerRepeatAspect
import io.gitee.zhangbinhub.acp.cloud.conf.AcpCloudLogServerClientConfiguration
import io.gitee.zhangbinhub.acp.cloud.conf.AcpCloudLogServerConfiguration
import io.gitee.zhangbinhub.acp.cloud.constant.AcpCloudConstant
import io.gitee.zhangbinhub.acp.cloud.serialnumber.GenerateSerialNumber
import io.gitee.zhangbinhub.acp.cloud.serialnumber.RedisGenerateSerialNumber
import io.gitee.zhangbinhub.acp.cloud.tools.CloudTools
import org.noear.solon.annotation.*
import org.noear.solon.core.aspect.MethodInterceptor
import org.redisson.api.RedissonClient
import org.redisson.solon.RedissonClientOriginalSupplier

/**
 * @since JDK 17
 */
@Configuration
@Import(AcpCloudLogServerConfiguration::class, AcpCloudLogServerClientConfiguration::class)
class AcpCloudComponentAutoConfiguration {
    @Bean
    fun cloudTools(@Inject serverTools: ServerTools) = CloudTools(serverTools)

    @Bean(injected = true)
    fun restControllerRepeatAspect(): MethodInterceptor = RestControllerRepeatAspect()

    @Bean
    fun redissonClient(@Inject("\${${AcpCloudConstant.REDIS_PROPERTIES_PREFIX}}") supplier: RedissonClientOriginalSupplier): RedissonClient {
        return supplier.get()
    }

    @Bean
    @Condition(onMissingBean = GenerateSerialNumber::class)
    fun redisGenerateSerialNumber(@Inject redissonClient: RedissonClient): GenerateSerialNumber =
        RedisGenerateSerialNumber(redissonClient)
}
